/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.custom.hvac;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.controller.hvac.FanSignalController;
import com.ecarx.systemui.plugin.utils.LogUtils;

public class HandFanControlView extends LinearLayout {

    private static final String TAG = "HandFanControlView";
    private LinearLayout rootView;
    private TextView tvSpeedValue;
    private SeekBar seekBar;
    private ImageView ivDown;
    private ImageView ivUp;

    private FanSignalController mFanSignalController;

    public HandFanControlView(Context context) {
        this(context, null);
    }

    public HandFanControlView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HandFanControlView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        mFanSignalController = FanSignalController.getInstance();
        LayoutInflater.from(getContext()).inflate(R.layout.dialog_hand_fan_speed, this, true);

        initView();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if(visibility == View.VISIBLE){
            boolean isSupportHandFan = mFanSignalController.isSupportHandFan();
            LogUtils.d(TAG, "isSupportHandFan:" + isSupportHandFan);
            if (isSupportHandFan) {
                Integer handFanValueLevel = mFanSignalController.getHandFanValueLevel();
                LogUtils.d(TAG, "handFanValueLevel:" + handFanValueLevel);
                updateProgress(handFanValueLevel);
            }
        }
    }

    private void initView() {
        rootView = findViewById(R.id.root_view);
        tvSpeedValue = findViewById(R.id.tv_speed_value);
        seekBar = findViewById(R.id.sb_fan_speed);
        seekBar.setMax(9);
        ivDown = findViewById(R.id.iv_down);
        ivUp = findViewById(R.id.iv_up);
        ivDown.setOnClickListener(v -> {
            mFanSignalController.setHandFanPreviousValue();
        });
        ivUp.setOnClickListener(v -> {
            mFanSignalController.setHandFanNextValue();
        });
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                LogUtils.d(TAG, "onProgressChanged progress:" + fromUser + "--fromUser:" + fromUser);
                if (fromUser) {
                    tvSpeedValue.setText(String.valueOf(progress));
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                LogUtils.d(TAG, "onStartTrackingTouch seekBar.progress:" + (seekBar.getProgress()));
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();
                LogUtils.d(TAG, "onStopTrackingTouch seekBar.progress:" + progress);

                mFanSignalController.setHandFanValue(progress);
            }
        });
    }

    public void updateProgress(Integer progress) {
        LogUtils.d(TAG, "updateProgress progress :" + progress);
        if (tvSpeedValue != null) {
            tvSpeedValue.setText(String.valueOf(progress));
        }
        if (seekBar != null) {
            seekBar.setProgress(progress);
        }
    }

}
